#!/bin/sh

#-------------------------------------#
#  SGT (Software Generation Toolkit)  #
#-------------------------------------#
#                                     #
#      Build W.E2P file               #
#                                     #
#  GDE 01/04/2004 v3.2                #
#       delete -int option            #
#                                     #
#  GDE 03/07/2003 v3.1                #
#       check \MAK subdirectory       #
#                                     #
#  GDE 22/04/2003 v3.0                #
#       improve registry              #
#       file creation                 #
#                                     #
#  GDE 28/02/2003 v2.4                #
#       add -clean  option            #
#                                     #
#  GDE 20/12/2002 v2.3                #
#       update -dwl option            #
#                                     #
#  GDE 02/12/2002 v2.2                #
#       add -le2p option              #
#                                     #
#  GDE 19/09/2002 v2.1                #
#       add -fdwl option              #
#                                     #
#  GDE 26/08/2002 v2.0                #
#       modify E2P managing           #
#                                     #
#  GDE 05/06/2002 v1.3                #
#       update the call of            #
#       make_e2p rules                #
#                                     #
#  GDE 14/05/2002 v1.2                #
#       update for SGT 1.1.1          #
#       update parameters             #
#                                     #
#  GDE 07/05/2002 v1.1                #
#       update for SGT 1.1.0          #
#                                     #
#  GDE 04/04/2002 v1.0                #
#       creation                      #
#                                     #
#-------------------------------------#
# $1: specific option                 #
#-------------------------------------#



#-------------------------------------#
# Script informations
#-------------------------------------#
export SCRIPT_SGT_VER=3.2
export SCRIPT_SGT_DATE=01/04/2004
export SCRIPT_SGT_USER=GDE
export SCRIPT_SGT_NAME=create_e2p
export SCRIPT_SGT_USAGE="Use GenE2P application to create some e2p and/or dwl files"
#-------------------------------------#




#---------------------
# Create and go to output directory
#---------------------
if [ -d mak ]
then
	mkdir -p -m775 out
	cd out
else
	export MSG_ERROR_SCRIPT=0
	export CMD_SCRIPT=Error
fi
#---------------------




#-------------------------------------#
# Parameters section
#-------------------------------------#
#---------------------
# For -reg option only
# Parameter 4 -> output file
#---------------------
if [ ! -z $4 ] && [ -z $CMD_SCRIPT ]
then
	export OUTPUT_FILE=$4
fi


#---------------------
# For -reg option only
# Parameter 3 -> input file
#---------------------
if [ ! -z $3 ] && [ -z $CMD_SCRIPT ]
then
	export INPUT_FILE=$4
fi


#---------------------
# Parameter 2 -> input file
#---------------------
if [ ! -z $2 ] && [ -z $CMD_SCRIPT ]
then
	if [ ! $1 = -reg ]
	then
		if [ -f ../mak/$2.mak ]
		then
			export INPUT_MAK=$2
		else
			if [ -f $2 ]
			then
				export INPUT_FILE=$2
			else
				export MSG_ERROR_SCRIPT=1
				export CMD_SCRIPT=Error
			fi
		fi
	else
		export STRUCTURE_NAME=$2
	fi
fi


#---------------------
# Parameter 1 -> GENE2P options
#---------------------
if [ ! -z $1 ] && [ -z $CMD_SCRIPT ]
then
	if [ $1 = -v ]
	then
		export CMD_SCRIPT=Version
	else
		if [ $1 = -h ]
		then
			export CMD_SCRIPT=Help
		else
			if [ $1 = --help ]
			then
				export CMD_SCRIPT=Help
			else
				if [ $1 = -hm ]
				then
					export CMD_SCRIPT=More
				else
					if [ $1 = -u ]
					then
						export CMD_SCRIPT=Usage
					fi
				fi
			fi
		fi
	fi
fi


#-------------------------------------#
# if no option is defined, display help
#-------------------------------------#
if [ $# -eq 0 ] && [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Help
fi


#-------------------------------------#
# Process section
#-------------------------------------#
#---------------------
# Call SGT with GenE2P application
#---------------------
if [ -z $CMD_SCRIPT ] && [ ! -z $1 ]
then
	#---------------------
	# Generate new w.e2p and w.dwl, e2p_csn.csn and e2p.h
	#---------------------
	if [ $1 = -e2p ] && [ ! -z $INPUT_MAK ]
	then
		echo " "
		echo "    $SCRIPT_SGT_USAGE"
		echo "    Generate 'w_*.e2p' and 'w_*.dwl', 'e2p_*.csn' and 'e2p_*.h'"
		echo " "
		make -f ../mak/gen.mak TARGET=$INPUT_MAK make_e2p
		exit
	fi

	#---------------------
	# Clean E2P files
	#---------------------
	if [ $1 = -clean ] && [ ! -z $INPUT_MAK ]
	then
		echo " "
		echo "    $SCRIPT_SGT_USAGE"
		echo "    Clean all files generated by GENE2P application"
		echo " "
		make -f ../mak/gen.mak TARGET=$INPUT_MAK make_e2p_clean
		exit
	fi

	#---------------------
	# Build compilated CSN1 file for TMT application (ex-MOKA)
	#---------------------
	if [ $1 = -cso ] && [ ! -z $INPUT_MAK ]
	then
		echo " "
		echo "    $SCRIPT_SGT_USAGE"
		echo "    Generate 'e2p_*.csn' and build 'e2p_*.cso'"
		echo " "
		make -f ../mak/gen.mak TARGET=$INPUT_MAK make_e2p
		make -f ../mak/gen.mak TARGET=$INPUT_MAK make_csn1
		exit
	fi

	#---------------------
	# Build some e2p files from a list
	#---------------------
	if [ $1 = -le2p ] && [ ! -z $INPUT_MAK ]
	then
		echo " "
		echo "    $SCRIPT_SGT_USAGE"
		echo "    Generate 'w_*.e2p' and 'w_*.dwl', 'e2p_*.csn' and 'e2p_*.h'"
		echo " "
		make -f ../mak/gen.mak TARGET=$INPUT_MAK MAKEFILE=makee2p.mak E2P_LIST_FOR_GENE2P=E2P_LIST_TO_CREATE_STACK GENE2P_LEVEL=level1 GENE2P_RULE=create_at_e2p_files use_gen2ep
		exit
	fi

	#---------------------
	# Convert some e2p files to dwl format
	#---------------------
	if [ $1 = -dwl ] && [ ! -z $INPUT_MAK ]
	then
		echo " "
		echo "    $SCRIPT_SGT_USAGE"
		echo "    Convert some e2p files to dwl format"
		echo " "
		if [ ! -z $3 ]
		then
			export FILE_PP=$3
			echo " "
			echo "    Convert some E2P files to dwl format using $FILE_PP"
			make -f ../mak/gen.mak TARGET=$INPUT_MAK MAKEFILE=makee2p.mak SRC_FILE_FOR_PP=$FILE_PP GENE2P_RULE=convert_e2p_to_dwl use_gen2ep
			exit
		else
			make -f ../mak/gen.mak TARGET=$INPUT_MAK make_pp
			make -f ../mak/gen.mak TARGET=$INPUT_MAK make_e2p_to_dwl
			exit
		fi
	fi

	#---------------------
	# Convert just one e2p files to dwl format
	#---------------------
	if [ $1 = -fdwl ] && [ ! -z $INPUT_FILE ]
	then
		if [ ! -z $3 ]
		then
			export FILE_PP=$3
		else
			export FILE_PP=$INPUT_FILE.pp
		fi
		echo " "
		echo "    Convert $INPUT_FILE file to dwl format using $FILE_PP"
		make -f ../mak/gen.mak TARGET=misc MAKEFILE=makee2p.mak USE_TARGET_FILE=OFF SRC_FILE_FOR_PP=$FILE_PP E2P_TO_DWL=$INPUT_FILE GENE2P_RULE=convert_one_e2p_file_to_dwl use_gen2ep
		exit
	fi

	#---------------------
	# Build registry file for TMT application
	#---------------------
	if [ $1 = -reg ]
	then
		if [ -z $STRUCTURE_NAME ]
		then
			export STRUCTURE_NAME=Registry_t
		fi
		if [ -z $INPUT_FILE ]
		then
			export INPUT_FILE=__register__.c
		fi
		if [ -z $OUTPUT_FILE ]
		then
			export OUTPUT_FILE=register.ini
		fi
		echo " "
		echo "    $SCRIPT_SGT_USAGE"
		echo "    Build registry file for TMT application"
		echo " "
		make -f ../mak/gen.mak TARGET=misc MAKEFILE=makee2p.mak USE_TARGET_FILE=OFF SRC_FILE_FOR_PP=$INPUT_FILE E2P_LIST_FOR_GENE2P=REGISTER_C make_c_file_for_pp_process
		make -f ../mak/gen.mak TARGET=misc MAKEFILE=makee2p.mak USE_TARGET_FILE=OFF SRC_FILE_FOR_PP=$INPUT_FILE make_pp_process
		make -f ../mak/gen.mak TARGET=misc MAKEFILE=makee2p.mak USE_TARGET_FILE=OFF SRC_FILE_FOR_PP=$INPUT_FILE GENE2P_OUT=$OUTPUT_FILE GENE2P_TOP=$STRUCTURE_NAME GENE2P_RULE=make_reg use_gen2ep
		exit
	fi
fi



#-------------------------------------#
# Help script section
#-------------------------------------#
#---------------------
# Help script section
#---------------------

if [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Version
fi


#---------------------
# Usage
#---------------------
if [ $CMD_SCRIPT = Usage ]
then
	echo "  $SCRIPT_SGT_NAME	$SCRIPT_SGT_USAGE"
	exit
fi

#---------------------
# Version
#---------------------
echo " "
echo "-------------------------------------"
echo " '$SCRIPT_SGT_NAME' (release "$SCRIPT_SGT_VER") for SGT "$SGT_VER". Last update by "$SCRIPT_SGT_USER" on "$SCRIPT_SGT_DATE
echo "  $SCRIPT_SGT_USAGE"
if [ $CMD_SCRIPT = Version ]
then
	exit
fi

#---------------------
# Simple help
#---------------------
if [ $CMD_SCRIPT = Help ]
then
	echo "-------------------------------------"
	echo " $SCRIPT_SGT_NAME [[BASIC_OPTIONS] | [ADVANCED_OPTIONS]] [PARAMETERS]"
	echo " "
	echo "  [BASIC_OPTIONS]"
	echo " "
	echo "    -e2p   Generate 'w_*.e2p' and 'w_*.dwl', 'e2p_*.csn' and 'e2p_*.h'"
	echo "    -cso   Idem that '-e2p' process but compile also the CSN1 file"
	echo "    -clean Clean all files generated by Gene2p"
	echo "    -le2p  Generate a list of E2P files for AT application."
	echo "    -dwl   Convert some E2P files to DWL format. FILE_PP is optional"
	echo "    -fdwl  Convert the current E2P file to DWL format."
	echo "           For this option, the input file is an E2P file"
	echo "   INPUT_FILE     : makefile (without mak extension)."
	echo "   FILE_PP        : file used for the pre-processing"
	echo " "
	echo "  [ADVANCED_OPTIONS]"
	echo " "
	echo "    -reg  Build registry file for TMT application"
	echo " "
	echo "   STRUCTURE_NAME : Structure to analyze (by default 'Registry_t')"
	echo "   INPUT_FILE     : C input file (by default '__register__.c')"
	echo "   OUTPUT_FILE    : Output file  (by default 'register.ini')"
	echo "-------------------------------------"
	echo "  -h | --help  Display this help "
	echo "  -hm          Display more help about SGT" $SGT_VER
	echo "  -v           Display the version of the current script"
fi

#---------------------
# More help
#---------------------
if [ $CMD_SCRIPT = More ]
then
	make -f ../mak/gen.mak help USE_TARGET_FILE=OFF
fi



#-------------------------------------#
# Error messages section
#-------------------------------------#
if [ $CMD_SCRIPT = Error ]
then
	if [ $MSG_ERROR_SCRIPT = 0 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : The root path is not a valid SGT directory. Please check for the \MAK sub-directory"
	fi
	if [ $MSG_ERROR_SCRIPT = 1 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : $1 or ../mak/$1.mak doesn't exist !"
	fi
	echo " "
	echo " Check your command line or see help section -> $SCRIPT_SGT_NAME -h"
fi